#!/bin/bash

# SfM Pipeline Runner Script
# This script sets up the environment and runs the SfM pipeline

set -e  # Exit on any error

# Default values
# DEFAULT_DATASET_DIR="/media/sdisk/data/3dgs/20250731/South-Building/images"
# DEFAULT_OUTPUT_DIR="/media/sdisk/data/3dgs/20250731/South-Building/outputs/sfm"
DEFAULT_DATASET_DIR="/media/sdisk/data/3dgs/220kv/images-selected/"
DEFAULT_OUTPUT_DIR="/media/sdisk/data/3dgs/220kv/sfm"

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -d|--dataset)
            DATASET_DIR="$2"
            shift 2
            ;;
        -o|--output)
            OUTPUT_DIR="$2"
            shift 2
            ;;
        -h|--help)
            echo "Usage: $0 [OPTIONS]"
            echo ""
            echo "Options:"
            echo "  -d, --dataset PATH    Path to the dataset images directory"
            echo "  -o, --output PATH     Path to the output directory"
            echo "  -h, --help           Show this help message"
            echo ""
            echo "Examples:"
            echo "  $0 -d /path/to/images"
            echo "  $0 -d /path/to/images -o /path/to/output"
            echo "  $0 -d /media/sdisk/data/3dgs/20250731/South-Building/images"
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            echo "Use -h or --help for usage information"
            exit 1
            ;;
    esac
done

# Set default values if not provided
DATASET_DIR=${DATASET_DIR:-$DEFAULT_DATASET_DIR}
OUTPUT_DIR=${OUTPUT_DIR:-$DEFAULT_OUTPUT_DIR}

echo "=== Hierarchical Localization SfM Pipeline ==="
echo "Dataset directory: $DATASET_DIR"
echo "Output directory: $OUTPUT_DIR"
echo "Starting pipeline execution..."

# Check if we're in the right directory
if [ ! -f "run_sfm.py" ]; then
    echo "Error: run_sfm.py not found. Please run from the project root directory."
    exit 1
fi

# Create necessary directories
mkdir -p "$OUTPUT_DIR"

# Check if dataset exists
if [ ! -d "$DATASET_DIR" ]; then
    echo "Error: Dataset directory does not exist: $DATASET_DIR"
    echo "Please ensure the dataset is available at the specified path."
    exit 1
fi

# Run the pipeline
echo "Running SfM pipeline..."
python3 run_sfm.py "$DATASET_DIR" --output "$OUTPUT_DIR"

echo "Pipeline execution completed!"
